package com.aaa.mythread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

public class MyThread13 implements Runnable {
    private int num;//
    private Semaphore semaphore;
    public MyThread13(int num, Semaphore semaphore) {
        this.num = num;
        this.semaphore = semaphore;
    }


    @Override
    public void run() {
        //
        try {

            semaphore.acquire();
            System.out.println("工人"+num+"开始使用机器");
            Thread.sleep(3000);
            System.out.println("工人"+num+"使用机器结束，释放机器的资源");
            semaphore.release();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {
       // 定义8 个工人
        Semaphore semaphore = new Semaphore(3);
        for(int i=1;i<9;i++){

            MyThread13 myThread13 = new MyThread13(i,semaphore);
            Thread thread = new Thread(myThread13);
            thread.start();
        }




    }
}
